"""
请求日志 | 接口
"""

from fastapi import (
    APIRouter,
    Depends,
)

import utils.enum_utils as enum
from biz import request_log_biz
from components.audit_log import audit
from components.depends import admin_required
from components.global_param import get_user_id
from components.logger import log
from components.response import OKWithDataResponse
from models.request import (
    RequestLogDetailRequest,
    RequestLogListRequest,
    RequestLogPathField,
)
from models.response import (
    RequestLogDetailResponse,
    RequestLogListResponse,
)
from models.vo import (
    RequestLogDetailVO,
    RequestLogListPage,
)
from utils.data_utils.special import ObjectIdStr

request_log_router: APIRouter = APIRouter(prefix="/request_log")
""" 请求日志 路由 """


@request_log_router.get(
    path="/{request_log_id}/",
    summary="请求日志 | 详情",
    response_model=RequestLogDetailResponse,
    dependencies=[Depends(admin_required)],
)
@audit(
    model=enum.Model.REQUEST_LOG,
    operation=enum.ModelOperation.DETAIL,
    po_id="request_log_id",
)
async def request_log_detail(request_log_id: ObjectIdStr = RequestLogPathField.request_log_id) -> OKWithDataResponse:
    request = RequestLogDetailRequest(request_log_id=request_log_id)
    vo: RequestLogDetailVO = request_log_biz.RequestLogGetter.detail(request)
    log.info(f"[请求日志][详情] user_id: {get_user_id()} request_log_id: {request_log_id}")

    return OKWithDataResponse(vo)


@request_log_router.get(
    path="/",
    summary="请求日志 | 列表",
    response_model=RequestLogListResponse,
    dependencies=[Depends(admin_required)],
)
@audit(
    model=enum.Model.REQUEST_LOG,
    operation=enum.ModelOperation.LIST,
    po_id="response.data.data_list.request_log_id",
)
async def request_log_list(request: RequestLogListRequest = Depends(RequestLogListRequest.from_query)) -> OKWithDataResponse:
    vo: RequestLogListPage = request_log_biz.RequestLogGetter.list(request)
    log.info(f"[请求日志][列表] user_id: {get_user_id()}")

    return OKWithDataResponse(vo)
